import { type NextRequest, NextResponse } from 'next/server';

export class NotesHandler {
    private notes = [
        { id: 1, content: "学习 Next.js API Routes" },
        { id: 2, content: "尝试 Tailwind CSS" },
        { id: 3, content: "构建一个全栈应用！" },
    ];

    handle = async (request: NextRequest) => {
        const method = request.method.toUpperCase();

        // 根据 HTTP 方法处理逻辑（GET/POST/PUT 等）
        if (method === 'GET') {
            // Simulate network delay
            await new Promise(resolve => setTimeout(resolve, 100));
            return NextResponse.json(this.notes);
        }

        if (method === 'POST') {
            try {
                const { content, action, id } = await request.json();

                // Handle delete action
                if (action === 'delete' && id) {
                    const noteId = parseInt(id, 10);
                    const initialLength = this.notes.length;
                    this.notes = this.notes.filter(note => note.id !== noteId);

                    if (this.notes.length === initialLength) {
                        return NextResponse.json({ error: 'Note not found' }, { status: 404 });
                    }

                    return new NextResponse(null, { status: 204 }); // 204 No Content 表示成功删除
                }

                // Handle create action (default)
                if (!content) {
                    return NextResponse.json({ error: 'Content is required' }, { status: 400 });
                }

                // Simulate network delay
                await new Promise(resolve => setTimeout(resolve, 100));

                const newNote = {
                    id: Date.now(),
                    content: content,
                };
                this.notes.push(newNote);

                return NextResponse.json(newNote, { status: 201 });
            } catch {
                return NextResponse.json({ error: 'Failed to process request' }, { status: 500 });
            }
        }

        // DELETE handler
        if (method === 'DELETE') {
            try {
                const { searchParams } = new URL(request.url);
                const id = searchParams.get('id');

                if (!id) {
                    return NextResponse.json({ error: 'ID is required' }, { status: 400 });
                }

                // Simulate network delay
                await new Promise(resolve => setTimeout(resolve, 100));

                const noteId = parseInt(id, 10);
                const initialLength = this.notes.length;
                this.notes = this.notes.filter(note => note.id !== noteId);

                if (this.notes.length === initialLength) {
                    return NextResponse.json({ error: 'Note not found' }, { status: 404 });
                }

                return new NextResponse(null, { status: 204 }); // 204 No Content 表示成功删除
            } catch {
                return NextResponse.json({ error: 'Failed to delete note' }, { status: 500 });
            }
        }

        return NextResponse.json({ error: 'Method not allowed' }, { status: 405 });
    };
}
